package com.example.himalaya.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.himalaya.R;
import com.example.himalaya.view.adapters.AnnouncerContentAdapter;
import com.example.himalaya.view.adapters.AnnouncerIndicatorAdapter;
import com.example.himalaya.view.callback.IAnnouncerDetailViewCallback;
import com.example.himalaya.presenters.AnnouncerDetailPresenter;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.text.MessageFormat;
import java.util.List;

public class AnnouncerActivity extends AppCompatActivity {

    private static final String TAG = "AnnouncerActivity";
    // UI Object
    private View mBtnBack;
    private ViewPager mAnnouncerPager;
    private TextView mFunsNumber;
    private TextView mNickName;


    // Other UI Object
    private MagicIndicator mAnnouncerIndicator;

    // Adapter
    private AnnouncerIndicatorAdapter mAnnouncerIndicatorAdapter;
    private CommonNavigator mCommonNavigator;


    // Presenter
    private AnnouncerDetailPresenter mAnnouncerDetailPresenter;
    private IAnnouncerDetailViewCallback mAnnouncerDetailViewCallback;
    private Announcer mCurrentAnnouncer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcer);


        // 设置上方的导航栏不显示
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        // 设置上方的StatusBar为透明
//        getWindow().setStatusBarColor(Color.TRANSPARENT);

        initVariable();


        initUiLoader();

        initAdapter();

        initPresenter();

        initListener();

        initCallback();

        initData();

        initView();
    }

    private void initCallback() {
        mAnnouncerDetailViewCallback = new IAnnouncerDetailViewCallback() {

            @Override
            public void onDetailListLoaded(List<Track> tracks) {

            }

            @Override
            public void onNetworkError(int errorCode, String errorMsg) {

            }

            @Override
            public void onAnnouncerLoaded(Announcer announcer) {

            }

            @Override
            public void onLoaderMoreFinished(int size) {

            }

            @Override
            public void onRefreshFinished(int size) {

            }
        };
        mAnnouncerDetailPresenter.registerViewCallback(mAnnouncerDetailViewCallback);
    }

    private void initAdapter() {
        // 创建对象
        // 创建一个indicator的适配器
        mAnnouncerIndicatorAdapter = new AnnouncerIndicatorAdapter(this);

        mCommonNavigator = new CommonNavigator(this);
        mCommonNavigator.setAdapter(mAnnouncerIndicatorAdapter);

        // 设置要显示的内容

        // 创建内容设配器
        FragmentManager fragmentManager = getSupportFragmentManager();
        AnnouncerContentAdapter announcerContentAdapter = new AnnouncerContentAdapter(fragmentManager);

        // 初始化
        mAnnouncerPager.setAdapter(announcerContentAdapter);
    }

    private void initUiLoader() {

    }

    private void initData() {
        mCurrentAnnouncer = mAnnouncerDetailPresenter.getCurrentAnnouncer();
    }

    private void initVariable() {
        mBtnBack = this.findViewById(R.id.back_btn);
        mAnnouncerPager = this.findViewById(R.id.announcer_pager);

        mNickName = this.findViewById(R.id.announcer_nickname);
        mFunsNumber = this.findViewById(R.id.announcer_fun_number_tv);

        // Other UI object
        mAnnouncerIndicator = this.findViewById(R.id.announcer_indicator);
        mAnnouncerIndicator.setBackgroundColor(this.getResources().getColor(R.color.color_main));
    }

    private void initPresenter() {
        mAnnouncerDetailPresenter = AnnouncerDetailPresenter.getInstance();
    }

    private void initListener() {

        mAnnouncerIndicatorAdapter.setOnIndicatorTapClickListener(new AnnouncerIndicatorAdapter.OnIndicatorTapClickListener() {
            @Override
            public void onTabClick(int index) {
                LogUtil.d(TAG, "Click index is ->" + index);
                if (mAnnouncerPager != null) {
                    mAnnouncerPager.setCurrentItem(index);
                }
            }
        });

        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initView() {
        // 把ViewPager和Indicator绑定到一起
        // 自动平分顶部
        mCommonNavigator.setAdjustMode(true);
        mAnnouncerIndicator.setNavigator(mCommonNavigator);
        ViewPagerHelper.bind(mAnnouncerIndicator, mAnnouncerPager);

        if (mCurrentAnnouncer != null) {
            mNickName.setText(mCurrentAnnouncer.getNickname());
            mFunsNumber.setText(MessageFormat.format("粉丝数{0}", mCurrentAnnouncer.getFollowerCount()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mAnnouncerDetailPresenter != null) {
            mAnnouncerDetailPresenter.unRegisterViewCallback(mAnnouncerDetailViewCallback);
        }
    }
}
